//
//  HaoBaMemoryDetailsVC.m
//  HaoBaShengxinEn
//
//  Created by luoy on 2024/6/19.
//

#import "HaoBaMemoryDetailsVC.h"
#import "HaoBaMemoryItem.h"

@interface HaoBaMemoryDetailsVC ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout> {
    NSTimer *_timer;
    NSInteger _second;
    NSMutableArray *_allNumbers;
    NSMutableArray *_needSelectNumbers;
    NSMutableArray *_selectedNumbers;
    BOOL _hideNumber;
    BOOL _hasMistake;
}
@property (weak, nonatomic) IBOutlet UILabel *psekeLb;
@property (weak, nonatomic) IBOutlet UILabel *gameEndTxt;

@property (weak, nonatomic) IBOutlet UIView *alertBgView;
@property (weak, nonatomic) IBOutlet UIView *alertView;
@property (weak, nonatomic) IBOutlet UILabel *tipsLb;
@property (weak, nonatomic) IBOutlet UILabel *secLb;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UIView *item1;
@property (weak, nonatomic) IBOutlet UIView *item2;
@property (weak, nonatomic) IBOutlet UIView *item3;
@property (weak, nonatomic) IBOutlet UILabel *item1Lb;
@property (weak, nonatomic) IBOutlet UILabel *item2Lb;
@property (weak, nonatomic) IBOutlet UILabel *item3Lb;

@end

@implementation HaoBaMemoryDetailsVC

- (IBAction)clickBack:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)backHome:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)replay:(UIButton *)sender {
    _second = 30;
    [self initNumbers];
    [self.collectionView reloadData];
    [self.alertBgView setHidden:YES];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:YES];
    [_timer invalidate];
    _timer = nil;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.alertView.layer.cornerRadius = 8;
    [self setBoldBorder:self.collectionView];
    [self setBoldBorder:self.item1];
    [self setBoldBorder:self.item2];
    [self setBoldBorder:self.item3];
    
    [self initNumbers];
    _timer = [NSTimer timerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) {
        [self setSecond];
    }];
    
    [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSDefaultRunLoopMode];
    [self.collectionView registerNib:[UINib nibWithNibName:@"HaoBaMemoryItem" bundle:nil] forCellWithReuseIdentifier:@"HaoBaMemoryItem"];
    _second = 30;
    [self showSelectItem:NO];
}

- (void)setBoldBorder:(UIView *)v {
    v.layer.borderWidth = 3;
    v.layer.borderColor = [UIColor colorWithRed:1 green:18/255 blue:85/255 alpha:1].CGColor;
}

- (void)setNumbers {
    int value = (arc4random() % 100) + 1;
    if ([_allNumbers containsObject:@(value)]) {
        [self setNumbers];
    } else {
        [_allNumbers addObject:@(value)];
    }
}

- (void)setNeedSelectNumbers {
    int value = (arc4random() % 16);
    
    if ([_needSelectNumbers containsObject:_allNumbers[value]]) {
        [self setNeedSelectNumbers];
    } else {
        [_needSelectNumbers addObject:_allNumbers[value]];
    }
}

- (void)initNumbers {
    _hasMistake = NO;
    [self.tipsLb setHidden:YES];
    [self.psekeLb setHidden:YES];
    [self.secLb setHidden:NO];
    [self showSelectItem:NO];
    _hideNumber = NO;
    _allNumbers = [NSMutableArray array];
    _needSelectNumbers = [NSMutableArray array];
    _selectedNumbers = [NSMutableArray array];
    for (int i = 0; i<16; i++) {
        [self setNumbers];
    }
    
    for (int i = 0; i<3; i++) {
        [self setNeedSelectNumbers];
    }
    
    _item1Lb.text = [NSString stringWithFormat:@"%2@",_needSelectNumbers[0]];
    _item2Lb.text = [NSString stringWithFormat:@"%2@",_needSelectNumbers[1]];
    _item3Lb.text = [NSString stringWithFormat:@"%2@",_needSelectNumbers[2]];
    
    [_timer setFireDate:[NSDate date]];
}

- (void)showSelectItem:(BOOL)show {
    [_item1 setHidden:!show];
    [_item2 setHidden:!show];
    [_item3 setHidden:!show];
}

- (void)setSecond {
    
    if (_second == -1) {
        [self gameStart];
        [_timer setFireDate:[NSDate distantFuture]];
        return;
    }
    self.secLb.text = [NSString stringWithFormat:@"%lds",(long)_second];
    _second --;
}

- (void)gameEnd:(BOOL)isSuccess {
    [self.alertBgView setHidden:NO];
    self.gameEndTxt.text = isSuccess ? @"恭喜你，挑战成功" : @"很遗憾，挑战失败";
}

- (void)gameStart {
    _hideNumber = YES;
    [self showSelectItem:YES];
    [self.collectionView reloadData];
    [self.psekeLb setHidden:NO];
    [self.secLb setHidden:YES];
    [self.tipsLb setHidden:NO];
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    HaoBaMemoryItem *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HaoBaMemoryItem" forIndexPath:indexPath];
    cell.numberLb.text = [NSString stringWithFormat:@"%@",_allNumbers[indexPath.row]];
    cell.contentView.layer.borderWidth = 0.5;
    cell.contentView.layer.borderColor = [UIColor colorWithRed:1 green:190/255 blue:208/255 alpha:1].CGColor;
    [cell.numberMask setHidden:!_hideNumber];
    if ([_selectedNumbers containsObject:_allNumbers[indexPath.row]]) {
        if ([_needSelectNumbers containsObject:_allNumbers[indexPath.row]]) {
            cell.contentView.backgroundColor = UIColor.whiteColor;
        } else {
            cell.contentView.backgroundColor = [UIColor colorWithRed:1 green:190/255 blue:208/255 alpha:0.25];
        }
        [cell.numberMask setHidden:YES];
    } else {
        cell.contentView.backgroundColor = UIColor.whiteColor;
    }
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CGFloat wid = self.view.bounds.size.width;
    return CGSizeMake((wid - 76) / 4, (wid - 76) / 4);
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 16;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (_second > 0) {
        return;
    }
    if ([_selectedNumbers containsObject:@(indexPath.row)]) {
        return;
    }
    [_selectedNumbers addObject:_allNumbers[indexPath.row]];
    [collectionView reloadItemsAtIndexPaths:@[indexPath]];
    if (![_needSelectNumbers containsObject:_allNumbers[indexPath.row]]) {
        _hasMistake = YES;
    }
    if (_selectedNumbers.count == 3) {
        [self gameEnd:!_hasMistake];
    }
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
}

@end
